package org.angelica.core.common;

import com.google.common.collect.Maps;
import lombok.Builder;
import lombok.Data;
import org.angelica.utils.common.JwtTokenUtil;

/**
 * @author aizhimin
 */
@Data
@Builder
public class AccessToken {
    /**
     * token
     */
    private String accessToken;
    /**
     * token过期时间
     */
    private Long expiredAt;
    /**
     * 刷新token
     */
    private String refreshToken;
    /**
     * 刷新token过期时间
     */
    private Long reExpiredAt;

    /**
     * 创建一个accessToken
     * @param userId
     * @return
     */
    public static AccessToken buildAccessToken(Long userId,String jwtSecret,String jwtIsuser){

        String accessToken = JwtTokenUtil.createAccessToken(jwtSecret,jwtIsuser,
                String.valueOf(userId), Maps.newHashMap(),Constant.JWT_TTL);

        String refreshToken = JwtTokenUtil.createAccessToken(jwtSecret,jwtIsuser,
                String.valueOf(userId), Maps.newHashMap(),Constant.JWT_REFRESH_TTL);

        return AccessToken.builder()
                .accessToken(accessToken)
                .expiredAt(JwtTokenUtil.getExpirationDateTime(jwtSecret, accessToken))
                .refreshToken(refreshToken)
                .reExpiredAt(JwtTokenUtil.getExpirationDateTime(jwtSecret, refreshToken))
                .build();
    }

}
